package com.aiwhose.aiwhosesecurity.utils;

import com.aiwhose.aiwhosecore.bean.BaseEntity;
import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.ColumnConfig;
import com.mybatisflex.codegen.config.EntityConfig;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.core.keygen.KeyGenerators;
import com.zaxxer.hikari.HikariDataSource;

public class Codegen {

    public static void main(String[] args) {
        // 直接设置服务名称为 "aiwhose-security"
        String serviceName = "aiwhose-security";

        // 配置数据源
        HikariDataSource dataSource = configureDataSource();

        // 创建配置内容
        GlobalConfig globalConfig = createGlobalConfig(serviceName);

        // 通过 datasource 和 globalConfig 创建代码生成器
        Generator generator = new Generator(dataSource, globalConfig);

        try {
            // 生成代码
            generator.generate();
            System.out.println("Code generation completed successfully for service: " + serviceName);
        } catch (Exception e) {
            System.err.println("Code generation failed: " + e.getMessage());
            e.printStackTrace();
        } finally {
            dataSource.close();
        }
    }

    private static HikariDataSource configureDataSource() {
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://192.144.233.163:3306/ai_security?characterEncoding=utf-8");
        dataSource.setUsername("root");
        dataSource.setPassword("1qaz!QAZ");
        return dataSource;
    }

    private static GlobalConfig createGlobalConfig(String serviceName) {
        // 创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();

        // 设置根包
        globalConfig.setBasePackage("com.aiwhose.aiwhosesecurity");
        globalConfig.setSourceDir(System.getProperty("user.dir") + "/" + serviceName + "/src/main/java");

        // 设置表前缀和只生成哪些表
        globalConfig.setTablePrefix("tb_");
        globalConfig.setGenerateTable("SHORT_MESSAGE");
        globalConfig.setLogicDeleteColumn("deleted"); // 逻辑删除

        // 设置生成 entity 并启用 Lombok
        globalConfig.setEntityGenerateEnable(true);
        globalConfig.setEntityWithLombok(true);
        globalConfig.setEntityJdkVersion(21);
        globalConfig.getEntityConfig()
                .setWithSwagger(true)
                .setSuperClass(BaseEntity.class);

        globalConfig.getEntityConfig().setOverwriteEnable(true);
        globalConfig.getEntityConfig().setSwaggerVersion(EntityConfig.SwaggerVersion.DOC);


        ColumnConfig columnConfig = new ColumnConfig();
        columnConfig.setColumnName("id")
                .isPrimaryKey();
        columnConfig.setKeyType(KeyType.Generator).setKeyValue(KeyGenerators.snowFlakeId); // id 生成器


        // 设置生成 mapper
        globalConfig.enableMapper();
        globalConfig.getMapperConfig().setMapperAnnotation(true);

        // 启用生成 Controller、Service 和 ServiceImpl
        globalConfig.enableController();
        globalConfig.enableService();
        globalConfig.enableServiceImpl();
        globalConfig.enableTableDef();

        // 注释配置 JavadocConfig
        globalConfig.getJavadocConfig().setAuthor("田国刚")  //
                .setSince("yyyy-MM-dd")
                .setSince("1.0.1");

        // 包配置 PackageConfig
        globalConfig.getPackageConfig()
                .setEntityPackage(globalConfig.getBasePackage() + ".bean")
                .setMapperPackage(globalConfig.getBasePackage() + ".mapper")
                .setServicePackage(globalConfig.getBasePackage() + ".service")
                .setControllerPackage(globalConfig.getBasePackage() + ".controller");

        return globalConfig;
    }
}
